<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

const animationDuration = 6000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    start:{
      type:Number,
      default: 50,
    },
    end:{
      type:Number,
      default:80
    },
    type:{
      type:Array,
      default:['type','type']
    },
    itemName:{
      type:Array,
      default:['itemName','itemName']
    },
    sumData:{
      type:Array,
      default:['sumData','sumData']
    },
    averageData:{
      type:Array,
      default:['averageData','averageData']
    },
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
            label: {
              show: true
            }
          }
        },
        toolbox: {
          show: true,
          feature: {
            // mark: { show: true },
            // dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        calculable: true,
        legend: {
          data: this.type,
          itemGap: 5,
          textStyle:{
            color: 'white'
          }
        },
        grid: {
          top: '18%',
          left: '1%',
          right: '10%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.itemName
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '时长',
          }
        ],
        dataZoom: [
          {
            show: true,
            start: this.start,
            end: this.end,
            textStyle:{
              color:"white"
            }
          },
          {
            type: 'inside',
            start: 94,
            end: 100
          },
          {
            show: true,
            yAxisIndex: 0,
            filterMode: 'empty',
            width: 30,
            height: '80%',
            showDataShadow: false,
            left: '93%'
          }
        ],
        series: [
          {
            name: this.type[0],
            type: 'bar',
            label:{
              show:true,
              position:"top",
              formatter:'{c}'
            },
            data: this.sumData
          },
          {
            name: this.type[1] ,
            type: 'bar',
            label:{
              show:true,
              position:"top",
              formatter:'{c}'
            },
            data: this.averageData
          }
        ]
      })
    }
  }
}

</script>
